/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.ui
{
	import com.competentum.engines.EmployeesEngine;
	import com.competentum.engines.InputOutputEngine;
	import com.competentum.engines.ProjectsEngine;
	import com.competentum.engines.ShortCutsEngine;
	import com.competentum.events.EWTParserCommandEvent;
	import com.competentum.events.EventReasons;
	import com.competentum.events.WorkloadEvent;
	import com.competentum.exceptions.NoShortcutError;
	import com.competentum.windows.Scope;
	import com.competentum.windows.ScopesManager;
	import com.competentum.lib.events.CommandEvent;
	import com.competentum.lib.events.ParserEngineCommandEvent;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mx.collections.ArrayCollection;

	public class Shortcuts
	{
		public static var shortCuts:Vector.<Shortcut> = new Vector.<Shortcut>();
		
		public static function get asArrayCollection():ArrayCollection
		{
			var result:ArrayCollection = new ArrayCollection();
			for (var i:int = 0; i < shortCuts.length; i ++ )
			{
				result.addItem(shortCuts[i]);
			}
			return result;
		}
		
		public static function init():void
		{
			var wEvent:WorkloadEvent;
			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.reason = EventReasons.NEW_WINDOW;
			shortCuts.push(new Shortcut('New window', [wEvent], Keyboard.N));

			var pEvent:EWTParserCommandEvent;
			
			pEvent = new EWTParserCommandEvent(CommandEvent.ACTIVATE_ENGINE);
			pEvent.commandType = InputOutputEngine.TYPE;
			pEvent.reason = ParserEngineCommandEvent.START_SAVING_CONFIGURATION;
			shortCuts.push(new Shortcut('Save', [pEvent], Keyboard.S));
			
			pEvent = new EWTParserCommandEvent(CommandEvent.ACTIVATE_ENGINE);
			pEvent.commandType = InputOutputEngine.TYPE;
			pEvent.reason = ParserEngineCommandEvent.START_SAVING_AS_CONFIGURATION;
			shortCuts.push(new Shortcut('Save As', [pEvent], Keyboard.S, true, true));
			
			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ShortCutsEngine.TYPE;
			wEvent.reason = EventReasons.LOAD_FILE;
			shortCuts.push(new Shortcut('Load', [wEvent], Keyboard.L));

			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ShortCutsEngine.TYPE;
			wEvent.reason = EventReasons.UNDO;
			shortCuts.push(new Shortcut('Undo', [wEvent], Keyboard.Z));

			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ShortCutsEngine.TYPE;
			wEvent.reason = EventReasons.REDO;
			shortCuts.push(new Shortcut('Redo', [wEvent], Keyboard.Y));
			
			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = EmployeesEngine.TYPE;
			wEvent.reason = EventReasons.REFRESH;
			var wEvent2:WorkloadEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent2.commandType = ProjectsEngine.TYPE;
			wEvent2.reason = EventReasons.REFRESH;
			shortCuts.push(new Shortcut('Refresh', [wEvent, wEvent2], Keyboard.R));

			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ShortCutsEngine.TYPE;
			wEvent.reason = EventReasons.SHOW_PREFERENCES;
			shortCuts.push(new Shortcut('Preferences', [wEvent], Keyboard.P));
		}
		
		private static function getActionByKeyboardEvent(event:KeyboardEvent):Array
		{
			for each (var shortCut:Shortcut in shortCuts)
			{
				if (shortCut.keyCode == event.keyCode && shortCut.alt == event.altKey && shortCut.ctrl == event.ctrlKey && shortCut.shift == event.shiftKey)
				{
					return shortCut.events;
				}
			}
			return null;
		}
		
		public static function invokeActions(shortcut:Shortcut):void
		{
			invokeEvents(shortcut.events);
		}
		
		public static function invokeActionsByKeyboardEvent(event:KeyboardEvent):void
		{
			var events:Array = getActionByKeyboardEvent(event);
			if (events)
			{
				invokeEvents(events);
			}
		}
		
		private static function invokeEvents(events:Array):void
		{
			for (var i:int = 0; i < events.length; i ++ )
			{
				var e:Object = events[i];
				e.scope = ScopesManager.currentScope;
				ScopesManager.currentWindow.dispatchEvent(e as Event);
			}
		}
		
		public static function getByTitle(title:String):Shortcut
		{
			for each (var shortCut:Shortcut in shortCuts)
			{
				if (shortCut.title == title)
				{
					return shortCut;
				}
			}
			throw new NoShortcutError();
		}
	}
}